set(ctf_reader_name "ctf_reader" CACHE INTERNAL
	"Name of CTF Reader library")

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

########################################################################
# If KEDR_GENERATE_CTF_META_PARSER is defined, files implementing
# parser and scanner of CTF metadata will be generated from bison and
# flex source files correspondingly.
# Otherwise files implemented parser and scanner are expected to be 
# already in source tree source and will be simply copied into binary one.
#
# In any case, build tree will contain parser and scanner files when
# the library is compiled.
########################################################################
option(KEDR_GENERATE_CTF_META_PARSER
	"Whether need to regenerate parser for CTF metadata." 
	OFF
)

# Because flex and bison generates header files, binary dir should
# be included for compile c-sources.
include_directories("${CMAKE_CURRENT_BINARY_DIR}")

# Name of source file implementing scanner without extension
set(ctf_meta_scanner_basename "ctf_reader_scanner_base")
# Same for parser
set(ctf_meta_parser_basename "ctf_reader_parser_base")

if(KEDR_GENERATE_CTF_META_PARSER)
    message(STATUS "Checking for flex scanner generator")
    find_program(FLEX flex)
    if(FLEX)
        message(STATUS "Checking for flex scanner generator - ${FLEX}")
    else(FLEX)
        message(FATAL_ERROR "Flex is not found")
    endif(FLEX)

    message(STATUS "Checking for bison parser generator")
    find_program(BISON bison)
    if(BISON)
        message(STATUS "Checking for bison parser generator - ${BISON}")
    else(BISON)
        message(FATAL_ERROR "Bison is not found")
    endif(BISON)

    add_custom_command(
        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_parser_basename}.tab.cc"
            "${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_parser_basename}.tab.hh"
            "${CMAKE_CURRENT_BINARY_DIR}/position.hh"
            "${CMAKE_CURRENT_BINARY_DIR}/location.hh"
			"${CMAKE_CURRENT_BINARY_DIR}/stack.hh"
        COMMAND ${BISON} -Wall -d --location
            "${CMAKE_CURRENT_SOURCE_DIR}/${ctf_meta_parser_basename}.yy"
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${ctf_meta_parser_basename}.yy"
    )
    # Currently don't use '-l' option to FLEX, otherwise -Wall produce
    # warnings. See
    # http://sourceforge.net/tracker/?func=detail&aid=3510440&group_id=97492&atid=618177
    # for details.
    add_custom_command(
        OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_scanner_basename}.cc"
        COMMAND ${FLEX} -o "${ctf_meta_scanner_basename}.cc"
            "${CMAKE_CURRENT_SOURCE_DIR}/${ctf_meta_scanner_basename}.l"
        DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${ctf_meta_scanner_basename}.l"
                "${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_parser_basename}.tab.hh"
    )
else(KEDR_GENERATE_CTF_META_PARSER)
    rule_copy_source("${ctf_meta_parser_basename}.tab.cc")
    rule_copy_source("${ctf_meta_parser_basename}.tab.hh")
    rule_copy_source("${ctf_meta_scanner_basename}.cc")
    rule_copy_source("position.hh")
    rule_copy_source("location.hh")
	rule_copy_source("stack.hh")
endif(KEDR_GENERATE_CTF_META_PARSER)

add_library(${ctf_reader_name} STATIC
	"ctf_context.cpp"
	"ctf_hash.cpp"
	"ctf_meta.cpp"
	"ctf_reader.cpp"
	"ctf_tag.cpp"
	"ctf_type.cpp"
	"ctf_types.cpp"
	"ctf_var.cpp"
	"ctf_var_place.cpp"
	"layout_support.cpp"
	"ctf_root_type.cpp"
	"ctf_reader_iter.cpp"
	"ctf_scope.cpp"
	"ctf_ast.cpp"
		
	"${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_scanner_basename}.cc"
	"${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_parser_basename}.tab.cc"
	
	"ctf_reader_builder.cpp"
)

# Comparision between positions in position.hh header generated by
# bison cause warning "suggest parentheses around ‘&&’ within ‘||’".
# So, every source which include this file should disable such warnings.
set_source_files_properties(
	"${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_parser_basename}.tab.cc"
	"ctf_reader_builder.cpp"
    PROPERTIES COMPILE_FLAGS "-Wno-parentheses")

# When generate reentrant scanner, additional argument may be not
# used by some LEX functions. Disable warnings about unused
# arguments when compile generated source.
#
# Also, because parser needs to be aware about locations, suppress
# "suggest parentheses..." warnings.
set_source_files_properties(
	"${CMAKE_CURRENT_BINARY_DIR}/${ctf_meta_scanner_basename}.cc"
    PROPERTIES COMPILE_FLAGS "-Wno-unused-parameter -Wno-parentheses")


# Tests
kedr_test_add_subdirectory(tests)